package de.tabuma.sso.project;

import de.tabuma.sso.*;
import de.tabuma.sso.group.*;
import de.tabuma.sso.sso.*;
import de.tabuma.sso.user.*;
import de.tabuma.sso.customer.*;
import de.tabuma.customer.entity.*;
import de.tabuma.customer.logik.*;
import de.tabuma.shopping.logik.*;

import java.util.*;

/**
 * @author $Author: alefen $
 * @version $Revision: 1.1.1.1.2.1 $
 * @since $Date: 2010/01/08 10:03:24 $
 */
public class ProjectWebservice {
    public static int create(Project project, String ssoId) throws Exception {

        int groupId = ProjectWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.PROJECT_CREATE, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }
        
        ProjectWebserviceLib.userHasPrivilegForProject(project, ssoId);
        return ProjectFacade.create(project, user.getName());

    }

    public static void update(Project project, String ssoId) throws Exception {

        int groupId = ProjectWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.PROJECT_UPDATE, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }

        ProjectWebserviceLib.userHasPrivilegForProject(project, ssoId);
        Project oldProject = ProjectFacade.get(project.getId().intValue());
        ProjectWebserviceLib.userHasPrivilegForProject(oldProject, ssoId);

        ProjectFacade.update(project, user.getName());
    }

    public static Project get(int id, String ssoId) throws Exception {
        int groupId = ProjectWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.PROJECT_GET, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }

        Project project = ProjectFacade.get(id);
        ProjectWebserviceLib.userHasPrivilegForProject(project, ssoId);
        return project;

    }

    public static void remove(int id, String ssoId) throws Exception {

        int groupId = ProjectWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.PROJECT_REMOVE, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }
        Project project = ProjectFacade.get(id);
        ProjectWebserviceLib.userHasPrivilegForProject(project, ssoId);
        
        ProjectFacade.remove(id, user.getName());

    }

    public static List<Project> getList(int customerId, String ssoId) throws Exception {

        int groupId = ProjectWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.PROJECT_GETLIST, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }

        Customer c = CustomerFacade.get(customerId);
        CustomerWebserviceLib.userHasPrivilegForCustomer(c, ssoId);
        return ProjectFacade.getList(customerId);
        
    }
}